package com.xmy.cultivate.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.PracticePlan;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.service.IGradeService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IStaffService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherController extends BaseController {

    @Autowired
    IStaffService iStaffService;

    @Autowired
    IGradeService iGradeService;

    @Autowired
    IQuarterService iQuarterService;

    @GetMapping("/getAllTeacher")
    public Result getAllTeacher(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                @RequestParam(value = "isTeacher", required = false) String isTeacher,
                                @RequestParam(value = "name", required = false) String name) {
        Page<Staff> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<Staff> staffQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(name)) {
            staffQueryWrapper.eq("is_teacher", 1);
        }
        if (isTeacher.equals("1")) {
            staffQueryWrapper.eq("is_teacher", 1);
        }

        staffQueryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        staffQueryWrapper.notLike("name", "前台");
        staffQueryWrapper.ne("status", 2);
        iStaffService.page(iPage, staffQueryWrapper);
        return new Result(Code.GET_OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), "获取成功");
    }


    @GetMapping("/getGradeTeacher")
    public Result getGradeTeacher(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                  @RequestParam(value = "yearPart", required = false) Integer yearPart,
                                  @RequestParam(value = "quarterNum", required = false) Integer quarterNum,
                                  @RequestParam(value = "schoolId", required = false) String schoolId,
                                  @RequestParam(value = "subjectsId", required = false) String subjectsId,
                                  @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                  @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                  @RequestParam(value = "name", required = false) String name) {

        Quarter quarterNow = iQuarterService.getNowQuarter();
        if (yearPart == null || quarterNum == null) {
            yearPart = quarterNow.getYearPart();
            quarterNum = quarterNow.getNum();
        }

        Page<Staff> iPage = new Page<>(pageNum, pageSize);

        iGradeService.getGradeTeacher(iPage, yearPart, quarterNum, schoolId, subjectsId, yearClassId, classTypeId, name);
        return new Result(Code.GET_OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), "获取成功");
    }


    /**
     * 获取所有员工
     *
     * @param name
     * @return
     */
    @GetMapping("/getAllTeacherList")
    public Result getAllTeacherList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
            @RequestParam(value = "name", required = false) String name) {

        Page<Staff> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("status", 2);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        iStaffService.page(iPage, queryWrapper);

        return new Result(Code.GET_OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), "获取成功");
    }


}
